package com.android.demo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class TestListView extends Activity{
	public ListView mlistView;
	LinearLayout ll;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.list_view);
		
		mlistView = (ListView)findViewById(R.id.list_view);
		
		ArrayList<String> array= new ArrayList<String>();
		for (int i=0;i<50;i++) {
			array.add(String.valueOf(i));
		}
		MyAdapter adapter = new MyAdapter();
		adapter.mItems = array;
		mlistView.setAdapter(adapter);
	}
	
	public class MyAdapter extends BaseAdapter{
		public ArrayList<String> mItems;
		@Override
		public int getCount() {
			return mItems.size();
		}

		@Override
		public Object getItem(int position) {
			return mItems.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView tt = new TextView(TestListView.this);
			tt.setText(getItem(position).toString());
			LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,50);
			tt.setLayoutParams(lp);
			tt.setOnTouchListener(new OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					mGesture.onTouchEvent(event);
					return false;
				}
			});
			return tt;
		}
	}
	
	private GestureDetector mGesture = new GestureDetector(new MyGestureDetector());
	public class MyGestureDetector extends SimpleOnGestureListener {
	    @Override
	    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
	    Log.i("TAG", "MyGestureDetector onFling");
	    	return true;
	    }
	    
	    @Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
	    	return super.onSingleTapConfirmed(e);
		}
	}
	
//	 public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent)
//	  {
//	    int i;
//	    if (paramInt == 4)
//	    {
//	      AlertDialog.Builder localBuilder1 = new AlertDialog.Builder(this).setMessage(2131230921);
//	      MainPage.10 local10 = new MainPage.10(this);
//	      AlertDialog.Builder localBuilder2 = localBuilder1.setPositiveButton(2131230917, local10);
//	      MainPage.11 local11 = new MainPage.11(this);
//	      localBuilder2.setNeutralButton(2131230918, local11).setTitle(2131230804).setNegativeButton(2131230783, null).show();
//	      i = 1;
//	    }
//	    while (true)
//	    {
//	      return i;
//	      if (paramInt == 84)
//	      {
//	        Intent localIntent;
//	        if ((DataHolder.account != null) && (DataHolder.account.isConnected()))
//	        {
//	          Context localContext = getApplicationContext();
//	          localIntent = new Intent(localContext, TopicPage.class);
//	          localIntent.addFlags(131072);
//	          if (this.chosenTab != 3)
//	            break label167;
//	          localIntent.putExtra("extra_name", "");
//	        }
//	        while (true)
//	        {
//	          startActivity(localIntent);
//	          i = 1;
//	          break;
//	          label167: localIntent.putExtra("extra_topic", "");
//	        }
//	      }
//	      if ((paramInt == 24) && (this.curDisplay != null))
//	      {
//	        this.curDisplay.moveUp();
//	        i = 1;
//	        continue;
//	      }
//	      if ((paramInt == 25) && (this.curDisplay != null))
//	      {
//	        this.curDisplay.moveDown();
//	        i = 1;
//	        continue;
//	      }
//	      boolean bool = onKeyDown(paramInt, paramKeyEvent);
//	    }
//	  }
//	 
	 public void moveDown()
	  {
	    
	      int firstPosition = mlistView.getFirstVisiblePosition()+1;	   
	      int count = this.mlistView.getCount() - 1;
	      if (firstPosition < count)
	      {
	        ListView localListView = mlistView;
	        localListView.setSelectionFromTop(firstPosition, 0);
	      }
	  }
	 
	 public void moveUp()
	  {
	 
	      int i = mlistView.getFirstVisiblePosition();
	      if (i - 1 >= 0)
	      {
	        ListView localListView = mlistView;
	        int j = i - 1;
	        localListView.setSelectionFromTop(j, 0);
	      }
	 
	  }
	 
	class Item extends Intent {
    	String s;
    	public Item(Class<?> c, String s) {
    		super(TestListView.this, c);
    		this.s = s;
		}
    	
    	@Override
    	public String toString() {
    		return s;
    	}
    }
	public ListAdapter mListAdapter = new ListAdapter() {
		
		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {			
		}
		
		@Override
		public void registerDataSetObserver(DataSetObserver observer) {
		}
		
		@Override
		public boolean isEmpty() {
			return false;
		}
		
		@Override
		public boolean hasStableIds() {
			return false;
		}
		
		@Override
		public int getViewTypeCount() {
			return 0;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			return null;
		}
		
		@Override
		public int getItemViewType(int position) {
			return 0;
		}
		
		@Override
		public long getItemId(int position) {
			return 0;
		}
		
		@Override
		public Object getItem(int position) {
			return null;
		}
		
		@Override
		public int getCount() {
			return 0;
		}
		
		@Override
		public boolean isEnabled(int position) {
			return false;
		}
		
		@Override
		public boolean areAllItemsEnabled() {
			return false;
		}
	};
	 
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}
}
